$(function() {
	// 初始化选中的标签
	var tab = $('.suredy-tabs>ul');
	if (tab.data('active-tab')) {
		$('.' + tab.data('active-tab') + '>a').tab('show');
	}

	// 初始化日期控件
	$.suredy.datetimepicker.init();

	// 刷新页面
	$.suredy.refreshContent = function(contractId, selector/* css选择器，可以为选择其的数组 */) {
		if (!contractId)
			return;

		var sls = [];

		if ($.isArray(selector)) {
			sls = selector;
		} else if (typeof selector === 'string') {
			sls[sls.length] = selector;
		}

		// 增加操作按钮的刷新
		sls[sls.length] = '#op-btns';
		// 增加操作日志的刷新
		sls[sls.length] = '.op-logs';

		var activeTabId = $('.suredy-tabs ul.nav-tabs>li.active').attr('id');

		var url = $.suredy.contextPath + '/contract/view/' + contractId;

		$.get(url, function(html, textStatus, jqXHR) {
			var $html = $(html);

			$.each(sls, function(i, val) {
				$(val).html($(val, $html).html());

				$('#' + activeTabId + '>a').tab('show');
			});
		}, 'html');
	};

	// 上传合同文件
	$('body').on('click', '.upload-contract-file', function() {
		$.suredy.showModal({
			icon : 'icon-upload',
			title : '上传相关资料',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/upload-contract-file/' + $(this).data('id')
		});
	});

	// 移除文件
	$('body').on('click', 'a.remove-contract-file', function(event) {
		var $this = $(this);
		var id = $this.data('id');
		var contractId = $this.data('contract-id');

		if (!id)
			alert('无效文件信息，无法删除！');

		$.ajax({
			url : $.suredy.contextPath + '/contract-file/del/' + id,
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '删除文件失败！');
					return false;
				}

				$.suredy.refreshContent(contractId, [ '#tab1' ]);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('删除文件失败！\n\n' + errorThrown);
			}
		});
	});

	// 开始履行合同
	$('body').on('click', '.start-contract', function(event) {
		var $this = $(this);

		$.suredy.showModal({
			size : 'lg',
			icon : 'icon-ok',
			title : '履行合同',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/start/' + $this.data('id')
		});
	});

	// 开始履行甲方合同
	$('body').on('click', '.start-contract-for-a', function(event) {
		var $this = $(this);

		$.suredy.showModal({
			size : 'lg',
			icon : 'icon-ok',
			title : '履行合同',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/start-for-a/' + $this.data('id')
		});
	});

	// 重置工期
	$('body').on('click', '.reset-project-time', function(event) {
		var $this = $(this);
		var id = $this.data('id');
		var start = $this.data('start') || 'null';
		var end = $this.data('end') || 'null';

		$.suredy.showModal({
			icon : 'icon-random',
			title : '重置工期',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/reset-project-time/' + id + '/' + start + '/' + end
		});
	});

	// 中止合同
	$('body').on('click', '.suspend-contract', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-remove-sign',
			title : '中止待建说明',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/update-status-only/' + $this.data('id') + '/SUSPEND'
		});
	});

	// 终止合同
	$('body').on('click', '.shutdown-contract', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-remove-sign',
			title : '终止合同说明',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/update-status-only/' + $this.data('id') + '/SHUTDOWN'
		});
	});

	// 复工
	$('body').on('click', '.redo-contract', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-remove-sign',
			title : '复工说明',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/update-status-only/' + $this.data('id') + '/START'
		});
	});

	// 分包合同
	$('body').on('click', '.do-subcontract', function() {
		var $this = $(this);

		$.suredy.showModal({
			size : 'lg',
			icon : 'icon-retweet',
			title : '创建子合同',
			showFoot : false,
			uri : $.suredy.contextPath + '/sub-contract/create/' + $this.data('id') + '/' + $this.data('type')
		});
	});

	// 收款
	$('body').on('click', '.pay-in', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-signin',
			title : '收款',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/pay-in/' + $this.data('id')
		});
	});

	// 收款计划
	$('body').on('click', '.payin-plan', function() {
		var $this = $(this);

		$.suredy.showModal({
			size : 'lg',
			icon : 'icon-money',
			title : '收款计划',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/payin-plan-multiple/' + $this.data('id')
		});
	});

	// 工期计划
	$('body').on('click', '.work-plan', function() {
		var $this = $(this);

		$.suredy.showModal({
			size : 'lg',
			icon : 'icon-building',
			title : '工期计划',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/work-plan-multiple/' + $this.data('id')
		});
	});

	// 竣工
	$('body').on('click', '.finish-project', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-bullhorn',
			title : '竣工',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/finish-contract/' + $this.data('id') + '?end=' + $this.data('end-time')
		});
	});

	// 完工结算
	$('body').on('click', '.clearing-contract', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-credit-card',
			title : '完工结算',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/clearing-contract/' + $this.data('id')
		});
	});

	// 通过收款计划收款
	$('body').on('click', '.get-back-money-by-plan .item', function() {
		var $this = $(this);
		var id = $this.data('id'); // 合同
		var planId = $this.data('plan-id'); // 合同
		var fp = $this.hasClass('yes'); // 发票已开

		$.ajax({
			url : $.suredy.contextPath + '/contract/get-back-money-by-plan',
			type : 'post',
			data : {
				id : id,
				planId : planId,
				faPiao : fp
			},
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '收款失败！');
					return false;
				}

				$.suredy.refreshContent(id, [ 'div.suredy-tabs' ]);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('收款失败！\n\n' + errorThrown);
			}
		});
	});

	// 完成工期计划
	$('body').on('click', '.work-completed', function() {
		var $this = $(this);
		var id = $this.data('id'); // 合同
		var planId = $this.data('plan-id'); // 合同

		$.ajax({
			url : $.suredy.contextPath + '/contract/work-completed-by-plan',
			type : 'post',
			data : {
				id : id,
				planId : planId
			},
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '操作失败！');
					return false;
				}

				$.suredy.refreshContent(id, [ 'div.suredy-tabs' ]);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('操作失败！\n\n' + errorThrown);
			}
		});
	});

	// 付款计划
	$('body').on('click', '.payout-plan-of-a', function() {
		var $this = $(this);

		$.suredy.showModal({
			size : 'lg',
			icon : 'icon-money',
			title : '付款计划',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/payout-plan-multiple/' + $this.data('id')
		});
	});

	// 编辑表单
	$('body').on('click', '.edit-form', function() {
		var $this = $(this);
		var id = $this.data('id');

		location.href = $.suredy.contextPath + "/contract/edit/" + id;
	});

	// 上传文件（通用）
	$('body').on('click', '.common-file-upload', function() {
		var $this = $(this);
		var contractId = $this.data('contract-id');
		var fileType = $this.data('file-type');
		var suffix = $this.data('suffix');

		$.suredy.showModal({
			icon : 'icon-upload',
			title : '文件上传',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/common-file-upload/' + contractId + '/' + fileType,
			data : {
				suffix : suffix
			}
		});
	});

	// 删除子合同
	$('body').on('click', '.delete-contract', function() {
		var $this = $(this);
		var id = $this.data('id');

		$.ajax({
			url : $.suredy.contextPath + '/contract/delete/' + id,
			type : 'post',
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '操作失败！');
					return false;
				}

				refresh();
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('操作失败！\n\n' + errorThrown);
			}
		});
	});

	// 删除工期计划
	$('body').on('click', '.delete-work-plan', function() {
		var $this = $(this);
		var id = $this.data('id');

		$.ajax({
			url : $.suredy.contextPath + '/contract/delete-work-plan/' + id,
			type : 'post',
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '操作失败！');
					return false;
				}

				refresh();
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('操作失败！\n\n' + errorThrown);
			}
		});
	});

	// 编辑工期计划
	$('body').on('click', '.edit-work-plan', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-building',
			title : '变更工期计划',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/work-plan-change?id=' + $this.data('id')
		});
	});

	// 删除收款计划
	$('body').on('click', '.delete-payin-plan', function() {
		var $this = $(this);
		var id = $this.data('id');

		$.ajax({
			url : $.suredy.contextPath + '/contract/delete-payin-plan/' + id,
			type : 'post',
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '操作失败！');
					return false;
				}

				refresh();
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('操作失败！\n\n' + errorThrown);
			}
		});
	});

	// 编辑收款计划
	$('body').on('click', '.edit-payin-plan', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-money',
			title : '变更收款计划',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/payin-plan-change/' + $this.data('id')
		});
	});

	// 删除付款计划
	$('body').on('click', '.delete-payout-plan', function() {
		var $this = $(this);
		var id = $this.data('id');

		$.ajax({
			url : $.suredy.contextPath + '/contract/delete-payout-plan/' + id,
			type : 'post',
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '操作失败！');
					return false;
				}

				refresh();
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('操作失败！\n\n' + errorThrown);
			}
		});
	});

	// 编辑付款计划
	$('body').on('click', '.edit-payout-plan', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-money',
			title : '变更付款计划',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/payout-plan-change/' + $this.data('id')
		});
	});

	// 费用支出
	$('body').on('click', '.pay-out', function() {
		$.suredy.showModal({
			icon : 'icon-money',
			title : '费用支出',
			showFoot : false,
			uri : $.suredy.contextPath + '/contract/cost-money/' + $(this).data('id')
		});
	});

	// 删除资金
	$('body').on('click', '.remove-money', function() {
		var $this = $(this);
		var id = $this.data('id');
		var contractId = $this.data('contract-id');

		$.ajax({
			url : $.suredy.contextPath + '/money/delete',
			type : 'post',
			dataType : 'json',
			data : {
				moneyId : id
			},
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '操作失败！');
					return false;
				}

				$.suredy.refreshContent(contractId, [ 'div#tab8' ]);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('操作失败！\n\n' + errorThrown);
			}
		});
	});

	// 合同退回
	$('body').on('click', '.back-contract', function() {
		var $this = $(this);
		var id = $this.data('id');

		$.ajax({
			url : $.suredy.contextPath + '/contract/back-contract',
			type : 'post',
			dataType : 'json',
			data : {
				id : id
			},
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '操作失败！');
					return false;
				}

				alert('退回成功，页面将关闭！');
				window.close();
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('操作失败！\n\n' + errorThrown);
			}
		});
	});

	// 工期变更记录
	$('body').popover({
		delay : {
			"show" : 200,
			"hide" : 100
		},
		selector : '.projecttime-logs',
		container : 'body',
		html : true,
		trigger : 'click',
		placement : 'right',
		template : '<div class="popover" role="tooltip" style="min-width: 600px; max-width: 600px; border-color: #989898;"><div class="arrow" style="border-right-color: #545454;"></div><h3 class="popover-title"></h3><div class="popover-content padding-0"></div></div>',
		title : '<b class="text-info"><i class="icon-random"></i> 变更记录</b>',
		content : function() {
			var html = $('div.projecttime-chang-info.hidden').html();
			return html;
		}
	});

});